import rclpy
from rclpy.node import Node
from std_msgs.msg import String
from university_interfaces.srv import GradeHomework

class TeacherNode(Node):

    def __init__(self, name):
        super().__init__(name)
        self.get_logger().info("我是%s, 我是一名老师!" % name)
        self.pub_hw = self.create_publisher(String, "homework", 10)
        self.i = 1
        self.timer_period = 2
        self.timer = self.create_timer(self.timer_period, self.timer_callback)

        self.grade_homework_service = self.create_service(GradeHomework, 'grade_homework', self.grade_homework_callback)

    def grade_homework_callback(self, req, res):
         name = req.name
         score = len(req.content)
         self.get_logger().info("作业批改：姓名：%s 得分：%d" % (name, score))

         res.score = score

         return res
    
    def timer_callback(self):
            msg = String()
            msg.data = "第%d次作业已发布,请查收。" % (self.i)
            self.pub_hw.publish(msg)
            self.get_logger().info('同学们，我发布了作业："%s"' % msg.data)
            self.i += 1

def main(args=None):
    rclpy.init(args=args)
    node = TeacherNode("zhangsan")
    rclpy.spin(node)
    rclpy.shutdown()
